<?php
namespace Home\Controller;
use Think\Controller;
class PostController extends Controller{
	//资料提交页面
	public function post()
	{
        $_SESSION['href']='Post/post';//记录当前的URL，实现登录后跳转到登录前界面
        header("Content-Type:text/html;charset=utf-8");
		//今天至前五天
		$today = date("Y-m-d");
        $yesterday = date("Y-m-d",strtotime("-1 day"));
        $twoDaysAgo = date("Y-m-d",strtotime("-2 day"));
        $threeDaysAgo = date("Y-m-d",strtotime("-3 day"));
        $fourDaysAgo = date("Y-m-d",strtotime("-4 day"));
        $fiveDaysAgo = date("Y-m-d",strtotime("-5 day"));

        $this -> assign('today',$today);
        $this -> assign('yesterday',$yesterday);
        $this -> assign('twoDaysAgo',$twoDaysAgo);
        $this -> assign('threeDaysAgo',$threeDaysAgo);
        $this -> assign('fourDaysAgo',$fourDaysAgo);
        $this -> assign('fiveDaysAgo',$fiveDaysAgo);

		//导航条的物品分类
		
		 $lost_thing_record = D('kind')->getKindName();
        $this->FoundRecord = $lost_thing_record;
        $username=I('get.username');
        $this->assign("username",$username);
       // echo $username;
		$this -> display();
	}

	//失物资料上传
	public function lost_upload()
	{
		header("Content-type: text/html; charset=utf-8");
        $m=M('user');
        $uid=$_SESSION['uid'];//通过uid去获取user表里对应的id
        $where['bbs_uid']=$uid;
        $id=$m->where($where)->field('id')->find();
        $id1=$id['id'];
        $data['user_id']=$id1;
		$lost_thing = M('thing');   //实例化模型类
		$data['name']       =    I("post.name1");        //丢失物品名
		$data['place']      =    I("post.place1");		//丢失物品的地方
		$data['lost_time']  =    I("post.lost_time1");	//丢失物品的时间
		$data['post_time']  =    date("Y-m-d H:i:s");   //招领发布时间
		$data['post_ip']    =    get_client_ip();       //客户端的ip
			//$Ip = new Org\Net\IpLocation('UTFWry.dat'); // 实例化类 参数表示IP地址库文件
			//$area = $Ip->getlocation('203.34.5.66'); // 获取某个IP地址所在的位置
		$data['kind_id']    = 	 I("post.kind1");        //丢失物品的种类id
		$data['description']=    I("post.description1"); //丢失物品的详细描述
		$data['contact']    =    I("post.contact1");     //联系方式
		$data['adward']     =    I("post.adward1");      //悬赏方式
		$data['addition']   =    I("post.addition1"); 	//补充的悬赏
		//图片上传
		$upload = new \Think\Upload();   //实例化上传类
		$upload -> maxSize    =    3145728 ;   //设置图片上传大小
		$upload -> exts       =    array('jpg','gif','png','jpeg');   //设置图片上传类型
		$upload -> rootPath   =    "./Public";     //设置图片上传根目录
		$upload -> savePath   =    "/Uploads/";    //设置图片上传保存目录
		$upload -> uploadReplace = false;          //存在同名文件是否覆盖
		//$upload -> thumbRemoveOrigin = false;      //生成缩略图是否删除雨原图
		//$upload -> thumb      = true;               //是否对图片文件进行缩略图处理，默认为false 
		 //上传图片
		 $info                 =    $upload -> upload();
		 $data['pic1']         =    $info['photo1']['savepath'].'/'.$info['photo1']['savename'];	//图片地址1
		 $data['pic2']         =    $info['photo2']['savepath'].'/'.$info['photo2']['savename'];	//图片地址2		
		 $data['pic3']         =    $info['photo3']['savepath'].'/'.$info['photo3']['savename'];	//图片地址3
		 $data['pic4']         =    $info['photo4']['savepath'].'/'.$info['photo4']['savename'];	//图片地址4
		 $data['pic5']         =    $info['photo5']['savepath'].'/'.$info['photo5']['savename'];	//图片地址5
		 $data['status']     =    0;                     // 0为未认领，1为已认领，2为过期，3为用户关闭
		 $data['lost_or_pick'] =  0;                    	//丢失或者捡到，丢失为0，捡到为1


		 $lastInsId = $lost_thing -> add($data);

		 //对trade_record表的操作
		$thing_trade_record = M('trade_record');  //实例化模型类
		$thing_record['thing_id']     = $lastInsId;         //最后插入的物品id
		$thing_record['post_id']      = $id1;    //发布招领的人的id
		$thing_record['request_id']   = 0;           //这里的request_id是要领取的人的id
		$thing_record['trade_time']   = date("Y-m-d H:i:s");  //录入时间、接了任务时间、完成时间都可以
		$thing_record['is_success']   = 0;           //0表示未完成，1表示已完成交易
		$lastInsId2 = $thing_trade_record -> add($thing_record);
		if  ($lastInsId or $lastInsId2) 
		{
            $this->FoundRecord=$data;
            $m=M('kind');
            $where1['id']=$data['kind_id'];
            $kind=$m->where($where1)->field('name')->find();
            $this->kind=$kind['name'];
            $this -> display('success');
		}
		else
		{
			$this -> error("上传失败");
		}

	}

	//拾物资料上传
	public function found_upload()
	{
		header("Content-type: text/html; charset=utf-8");
        $m=M('user');
        $uid=$_SESSION['uid'];//通过uid去获取user表里对应的id
        $where['bbs_uid']=$uid;
        $id=$m->where($where)->field('id')->find();
        $id1=$id['id'];
        $data['user_id']=$id1;
		$lost_thing = M('thing');   //实例化模型类

		//对thing表的操作
		$data['name']       =    I("post.name2");        //丢失物品名
		$data['place']      =    I("post.place2");		//丢失物品的地方
		$data['lost_time']  =    I("post.lost_time2");	//丢失物品的时间
		$data['post_time']  =    date("Y-m-d H:i:s");   //招领发布时间
		$data['post_ip']    =    get_client_ip();       //客户端的ip
			//$Ip = new Org\Net\IpLocation('UTFWry.dat'); // 实例化类 参数表示IP地址库文件
			//$area = $Ip->getlocation('203.34.5.66'); // 获取某个IP地址所在的位置
		$data['kind_id']    = 	 I("post.kind2");        //丢失物品的种类id
		$data['description']=    I("post.description2"); //丢失物品的详细描述
		$data['contact']    =    I("post.contact2");     //联系方式
		//$data['adward']     =    I("post.adward2");      //悬赏方式
		//$data['addition']   =    I("post.addition2"); 	//补充的悬赏

		//图片上传
		$upload = new \Think\Upload();   //实例化上传类
		$upload -> maxSize    =    3145728 ;   //设置图片上传大小
		$upload -> exts       =    array('jpg','gif','png','jpeg');   //设置图片上传类型
		$upload -> rootPath   =    "./Public";     //设置图片上传根目录
		$upload -> savePath   =    "/Uploads/";    //设置图片上传保存目录
		$upload -> uploadReplace = false;          //存在同名文件是否覆盖
		//$upload -> thumbRemoveOrigin = false;      //生成缩略图是否删除雨原图
		//$upload -> thumb      = true;               //是否对图片文件进行缩略图处理，默认为false
		 //上传图片
		 $info                 =    $upload -> upload();
		 $data['pic1']         =    $info['photo1']['savepath'].'/'.$info['photo1']['savename'];	//图片地址1
		 $data['pic2']         =    $info['photo2']['savepath'].'/'.$info['photo2']['savename'];	//图片地址2
		 $data['pic3']         =    $info['photo3']['savepath'].'/'.$info['photo3']['savename'];	//图片地址3
		 $data['pic4']         =    $info['photo4']['savepath'].'/'.$info['photo4']['savename'];	//图片地址4
		 $data['pic5']         =    $info['photo5']['savepath'].'/'.$info['photo5']['savename'];	//图片地址5
		 $data['status']     =    0;                     // 0为未认领，1为已认领，2为过期，3为用户关闭
		 $data['lost_or_pick'] =  1;                    	//丢失或者捡到，丢失为0，捡到为1
		 $lastInsId = $lost_thing -> add($data);

		 //对trade_record表的操作
		$thing_trade_record = M('trade_record');  //实例化模型类
		$thing_record['thing_id']     = $lastInsId;         //最后插入的物品id
		$thing_record['post_id']      = $id1;    //发布招领的人的id
		$thing_record['request_id']   = 0;           //这里的request_id是领取任务的人的ip
		$thing_record['trade_time']   = date("Y-m-d H:i:s");  //录入时间、接了任务时间、完成时间都可以
		$thing_record['is_success']   = 0;           //0表示未完成，1表示已完成交易
		$lastInsId2 = $thing_trade_record -> add($thing_record);
		if  ($lastInsId or $lastInsId2)
		{
            $this->FoundRecord=$data;
            $m=M('kind');
            $where1['id']=$data['kind_id'];
           $kind=$m->where($where1)->field('name')->find();
            $this->kind=$kind['name'];
			$this -> display('success');
		}
		else
		{
			$this -> error("上传失败");
		}
	 }
    public function  success()
    {
        $this->display();
    }
      function _empty(){
        header("HTTP/1.0 404 Not Found");//使HTTP返回404状态码
        $this->display("Error:error404");
    }
}